package org.jeecgframework.web.service.impl;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.service.ZyEquipmentTypeServiceI;
import org.jeecgframework.web.system.pojo.base.ZyEquipmentTypeEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("zyEquipmentTypeService")
@Transactional
public class ZyEquipmentTypeServiceImpl extends CommonServiceImpl implements ZyEquipmentTypeServiceI {

	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((ZyEquipmentTypeEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((ZyEquipmentTypeEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((ZyEquipmentTypeEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @param id
	 * @return
	 */
 	public boolean doAddSql(ZyEquipmentTypeEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @param id
	 * @return
	 */
 	public boolean doUpdateSql(ZyEquipmentTypeEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @param id
	 * @return
	 */
 	public boolean doDelSql(ZyEquipmentTypeEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,ZyEquipmentTypeEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{type_desc}",String.valueOf(t.getTypeDesc()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
	@Override
	public List<Map<String, Object>> queryEsTypeByPTypeDescAndPPTypeDesc(
			HttpServletRequest request, String typeDesc, String pTypeDesc) {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT ");
		sql.append(" 	et.id, et.type_desc AS item ");//type_desc AS 是因为 备件的那块叫 item ，方便循环
		sql.append(" FROM ");
		sql.append(" 	zy_equipment_type et ");
		sql.append(" LEFT JOIN zy_equipment_type pet ON pet.id = et.parent_id ");
		sql.append(" LEFT JOIN zy_equipment_type ppet ON ppet.id = pet.parent_id ");
		sql.append(" WHERE ");
		sql.append(" 	ppet.type_desc = '").append(pTypeDesc).append("' ");
		if(StringUtil.isNotEmpty(typeDesc)){//如果没有规定父级的typeDesc 那么只根据父父级typeDesc查询 所有最低级子集
			sql.append(" AND pet.type_desc = '").append(typeDesc).append("' ");
		}
//		System.out.println("+++++++++++++++++++巡检报告 查询TMS和服务器型号SQL++++++begin++++++++++++++++++++++++");
//		System.out.println(sql.toString());
//		System.out.println("+++++++++++++++++++巡检报告 查询TMS和服务器型号SQL+++++++end+++++++++++++++++++++++");
		return super.findForJdbc(sql.toString());
	}

}